package com.example.unknowntext.db;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import com.example.unknowntext.data.Comment;
import com.example.unknowntext.data.MainData;
import com.example.unknowntext.data.User;
import com.example.unknowntext.db.DBHelper.FavTable;
import com.example.unknowntext.db.DBHelper.UserInfo;
import com.example.unknowntext.util.DesUtil;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class DatabaseUtil {
	private static DatabaseUtil instance;

	/** 数据库帮助类 **/
	private DBHelper dbHelper;

	public synchronized static DatabaseUtil getInstance(Context context) {
		if (instance == null) {
			instance = new DatabaseUtil(context);
		}
		return instance;
	}

	/**
	 * 初始化
	 * 
	 * @param context
	 */
	private DatabaseUtil(Context context) {
		dbHelper = new DBHelper(context);
	}

	/**
	 * 
	 */
	public static void destory() {
		if (instance != null) {
			instance.onDestory();
		}
	}

	/**
	 * 关闭数据库
	 */
	public void onDestory() {
		instance = null;
		if (dbHelper != null) {
			dbHelper.close();
			dbHelper = null;
		}
	}

	// 保存用户头像
	public void insertUserIcon(Bitmap bitmap, String username) {
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
		String where = UserInfo.USER_ID + " = '" + username + "'";
		ContentValues conv = new ContentValues();
		conv.put(UserInfo.HEAD_PIC, os.toByteArray());
		if (isExistUser(username)) {
			dbHelper.update(DBHelper.TABLE_NAME_USER, conv, where, null);
		} else {
			dbHelper.insert(DBHelper.TABLE_NAME_USER, null, conv);
		}
		onDestory();
	}

	// 查询用户头像
	public Bitmap queryUserIcon(String username) {
		Bitmap bitmap = null;
		Cursor cursor = null;
		String where = UserInfo.USER_ID + " = '" + username + "'";
		cursor = dbHelper.query(DBHelper.TABLE_NAME_USER, null, where, null, null, null, null);
		if (cursor != null && cursor.getCount() > 0) {
			cursor.moveToFirst();
			byte[] data = cursor.getBlob(cursor.getColumnIndex(UserInfo.HEAD_PIC));
			if (data == null) {
				onDestory();
				return null;
			} else {
				bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
				onDestory();
				return bitmap;
			}
		} else {
			onDestory();
			bitmap = null;
		}
		onDestory();
		return bitmap;
	}

	// 查询用户信息
	public User queryUser(String username) {
		Cursor cursor = null;
		User user = new User();
		String where = UserInfo.USER_ID + " = '" + username + "'";
		cursor = dbHelper.query(DBHelper.TABLE_NAME_USER, null, where, null, null, null, null);

		if (cursor != null && cursor.getCount() > 0) {
			cursor.moveToFirst();
			user.setUsername(cursor.getString(cursor.getColumnIndex(UserInfo.USER_ID)));
			user.setNick(cursor.getString(cursor.getColumnIndex(UserInfo.NICK)));
			user.setEmail(cursor.getString(cursor.getColumnIndex(UserInfo.EMAIL)));
			user.setMobilePhoneNumber(cursor.getInt(cursor.getColumnIndex(UserInfo.PHONE)) + "");
			user.setSex(cursor.getInt(cursor.getColumnIndex(UserInfo.SEX)) == 0 ? false : true);
			try {
				user.setPassword(DesUtil.decrypt(cursor.getString(cursor.getColumnIndex(UserInfo.PSW)), "$unknowntext$" + DBHelper.CODE));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		onDestory();
		return user;
	}


	// 插入用户
	public void InertUser(User user, String psw) {
		String where = UserInfo.USER_ID + " = '" + user.getUsername() + "'";
		ContentValues conv = new ContentValues();
		conv.put(UserInfo.OBJECT_ID, user.getObjectId());
		conv.put(UserInfo.USER_ID, user.getUsername());
		try {
			conv.put(UserInfo.PSW, DesUtil.encrypt(psw, "$unknowntext$" + DBHelper.CODE));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		conv.put(UserInfo.SEX, user.isSex() ? 1 : 0);
		conv.put(UserInfo.PHONE, user.getMobilePhoneNumber() == null ? "" : user.getMobilePhoneNumber());
		conv.put(UserInfo.EMAIL, user.getEmail() == null ? "" : user.getEmail());
		conv.put(UserInfo.SIGNATURE, user.getSignature() == null ? "" : user.getSignature());
		if (isExistUser(user.getUsername())) {
			dbHelper.update(DBHelper.TABLE_NAME_USER, conv, where, null);
		} else {
			dbHelper.insert(DBHelper.TABLE_NAME_USER, null, conv);
		}
		onDestory();
	}

	// 是否存在该用户
	public boolean isExistUser(String username) {
		Cursor cursor = null;
		String where = UserInfo.USER_ID + " = '" + username + "'";
		cursor = dbHelper.query(DBHelper.TABLE_NAME_USER, null, where, null, null, null, null);
		if (cursor != null && cursor.getCount() > 0) {
			cursor.moveToFirst();
			return true;
		}
		return false;
	}

	// 点赞
	public void insertPraise(MainData data) {
		ContentValues conv = new ContentValues();
		conv.put(FavTable.IS_PRAISE, 1);
		conv.put(FavTable.OBJECT_ID, data.getObjectId() + FavTable.IS_PRAISE);
		dbHelper.insert(DBHelper.TABLE_NAME, null, conv);
		onDestory();
	}

	// 收藏
	public void insertFav(Comment data) {
		ContentValues conv = new ContentValues();
		conv.put(FavTable.OBJECT_ID, data.getObjectId() + FavTable.IS_FAV);
		conv.put(FavTable.IS_FAV, 1);
		dbHelper.insert(DBHelper.TABLE_NAME, null, conv);
		onDestory();
	}

	/**
	 * 
	 * @Title: isPraise
	 * @Description: TODO(是否已点赞)
	 * @param @param data
	 * @param @return 设定文件
	 * @return boolean 返回类型
	 * @throws
	 */
	public boolean isPraise(MainData data) {
		Cursor cursor = null;
		String where = FavTable.OBJECT_ID + " = '" + data.getObjectId() + FavTable.IS_PRAISE + "'";
		cursor = dbHelper.query(DBHelper.TABLE_NAME, null, where, null, null, null, null);
		if (cursor != null && cursor.getCount() > 0) {
			cursor.moveToFirst();
			if (cursor.getInt(cursor.getColumnIndex(FavTable.IS_PRAISE)) == 1) {
				return true;
			}
		}
		return false;
	}

	public boolean isFav(Comment data) {
		Cursor cursor = null;
		String where = FavTable.OBJECT_ID + " = '" + data.getObjectId() + FavTable.IS_FAV + "'";
		cursor = dbHelper.query(DBHelper.TABLE_NAME, null, where, null, null, null, null);
		if (cursor != null && cursor.getCount() > 0) {
			cursor.moveToFirst();
			if (cursor.getInt(cursor.getColumnIndex(FavTable.IS_FAV)) == 1) {
				return true;
			}
		}
		return false;
	}

}
